﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack;
using ServiceStack.Text;
using ServiceStack.Caching;
using ServiceStack.Redis.Generic;

namespace RedisStudy
{
    class Program
    {

        static void Main(string[] args)
        {
            List<string> listWrite = new List<string>() { "6380@192.168.8.245:6380" };
            List<string> readHosts = new List<string>() { "192.168.8.245:6381", "192.168.8.245:6382" };
            PooledRedisClientManager clientManager = PoolManagerFactory.CreateManager(listWrite.ToArray(), readHosts.ToArray());
            ///可以在缓存管理器中加入密码验证  因为没有对应的密码字段显示
            using (IRedisClient redisClient = clientManager.GetReadOnlyClient())
            {                
                
                IRedisTypedClient<Phone> phones = redisClient.As<Phone>();
                
                Phone phoneFive = phones.GetValue("5");
                if (phoneFive == null)
                {
                    phoneFive = new Phone()
                    {
                        ID = 5,
                        Manufacturer = "Nokia",
                        Model = "guozhiqi",
                        Owner = new Person()
                        {

                            ID = 1,
                            Name = "袁金州",
                            Surname = "Old"
                        }
                    };
                    phones.SetEntry(phoneFive.ID.ToString(), phoneFive);
                }
                Console.WriteLine("OwnerID" + phones.GetValue("5").Owner.Name);
            }


            Console.ReadKey();

        }



    }
    /// <summary>
    /// 缓存客户端管理器工厂
    /// </summary>
    public class PoolManagerFactory
    {
        private static PooledRedisClientManager Manager = null;
        public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts, int initialDB = 0)
        {
            if (Manager == null)
            {
                Manager = new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig()
                {
                    MaxWritePoolSize = 5,
                    MaxReadPoolSize = 5,
                    AutoStart = true
                }, initialDB, 50, 5);
            }
            return Manager;
        }

    }

    public class Phone
    {
        public int ID { get; set; }
        public string Model { get; set; }
        public string Manufacturer { get; set; }
        public Person Owner { get; set; }
    }

    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }

}
